
/* Replicates the figures and tables in the online appendix, 
	except Figure A1, and Tables A2, A3, and A15, for which 
	separate do files are provided. */

clear *

* define programs to be used
* setup
program define setup
	use survey, clear
	do "-controls.do"
	gen wt = 1
	* setting up macros (used in some of the analyses)
	global cond
	global weight
	global add
end

* get coefficients, standard errors, and p-values
program define getcoef
	args x j w /* x = variable, j = number of matrix, w = weight*/
	reg `x' i.t15 $controls1 [pw = `w'], r
	mat obs`j' = e(N)
	* all coefficients
	mat coef`j' = e(b)
	mat var`j' = vecdiag(e(V))
	matmap var`j' se`j', m(sqrt(@))
	mat pval`j' = r(table)
	local C = colsof(pval`j')
	mat pval`j' = pval`j'[4..4,1..`C']
	mat pval`j' = pval`j''
	local R = rowsof(pval`j')
	mat empty`j' = J(`R',1,.)
	mat coef`j' = coef`j'', se`j''
	* class-only + pred. value for rest
	mat coefi`j' = coef`j'[2..4,1..2]
	mat pvali`j' = pval`j'[2..4,1..1]
	mat coefi`j' = coefi`j',pvali`j'
	margins, at(t15 = 1) post
	mat pred`j' = (_b[_cons],_se[_cons],.)
	mat coefi`j' = coefi`j' \ pred`j'
end

* merge all appropriate models into one table
program define coefmerge
	args m n /* m = matrix name, n = number of columns */
	mat `m' = `m'1
	forval i = 2/`n' {
		mat `m' = `m',`m'`i'
		}
end

* organize full coeficient table
program define matclean
	clear
	svmat coef
	svmat pval
	gen names = ""
	local names : rownames coef
	local k = 1
	foreach j of local names {
		replace names = "`j'" in `k'
		local k = `k' + 1
		}
	egen base = rowtotal(coef*)
	drop if base == 0
	drop base
	local m 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
	gen order = .
	local k = 1
	foreach j of local m {
		replace order = `j' in `k'
		local k = `k' + 1
		}
	local n = _N
	local newn = `n'+2
	set obs `newn'
	local m 1 5
	local k = 1
	foreach j of local m {
		local newn = `n'+`k'
		replace order = `j' in `newn'
		local k = `k' + 1
		}
	sort order
	mkmat coef*, mat(coef)
	mkmat pval*, mat(pval)
end

* assign significance symbols
program define getstars
	args m s /* m = pval matrix name, s = stars matrix name */
	local cols = colsof(`m')
	local rows = rowsof(`m')
	mat `s' = J(`rows',`cols',0)

	forval i = 1/`rows' {
		forval j = 1/`cols' {
			local pval = `m'[`i',`j']
		if `pval' <= .10 & `pval' > .05 {
			matrix `s'[`i',`j'] = 1
			}
		if `pval' <= .05 & `pval' > .01 {
			matrix `s'[`i',`j'] = 2 
			}
		if `pval' <= .01 {
			matrix `s'[`i',`j'] = 3
			}
		}
	}
end

* table with all coefs for main models
program define tablel
	frmttable, clear
	frmttable, statmat(coef) substat(1) sdec(2) annotate(stars) asymbol(*,**,***) ///
		rtitles("\textbf{Economic class}" \ "" \ "\hspace{1em} Highly affluent (top 1\%)" \ "" \ ///
		"\hspace{1em} Affluent (95-99\%)" \ "" \ "\hspace{1em} Upper middle class ($\sim$80-95\%)" \ "" \ ///
		"\textbf{Socio-demographic controls}" \ "" \ ///
		"\hspace{1em} Age" \ "" \ "\hspace{1em} Female" \ "" \ "\hspace{1em} White" \ "" \ ///
		"\hspace{1em} Retired" \ "" \ "\hspace{1em} Married" \ "" \ ///
		"\hspace{1em} Education--High school" \ "" \ ///
		"\hspace{1em} Education--Some college" \ "" \ ///
		"\hspace{1em} Education--2-year college" \ "" \ ///
		"\hspace{1em} Education--4-year college" \ "" \ ///
		"\hspace{1em} Education--Post-grad" \ "" \ ///
		"\hspace{1em} Region--Northeast" \ "" \ ///
		"\hspace{1em} Region--Midwest" \ "" \ ///
		"\hspace{1em} Region--West" \ "" \ ///
		"\hspace{1em} Church attendance" \ "" \ ///
		"Constant" \ "")
	frmttable, statmat(obs) substat(0) sdec(0) rtitle("N") append
end

* shortened table for main models
program define tables
	frmttable, clear
	frmttable, statmat(coefi) substat(2) sdec(2) ///
		rtitles("Highly affluent (top 1\%)" \ "" \ "" \ "Affluent (95-99\%)" \ "" \ "" \ ///
		"Upper middle class ($\sim$80-95\%)" \ "" \ "" \ "General population" \ "---predicted value" \ "")
	frmttable, statmat(obs) substat(0) sdec(0) rtitle("N") append
end

* program for additional controls
program define addcons
	args v /* v = dep var. */
	setup
	forval j = 1/4 {
		qui reg `v' i.t15 ${rcons`j'}, r
		mat obs`j' = e(N),.
		mat coef`j' = e(b)
		mat var`j' = vecdiag(e(V))
		matmap var`j' se`j', m(sqrt(@))
		mat pval`j' = r(table)
		local C = colsof(pval`j')
		mat pval`j' = pval`j'[4..4,1..`C']
		mat pval`j' = pval`j''
		mat coef`j' = coef`j'', se`j''
		scalar R = rowsof(coef`j')-1
		scalar A = rowsof(coef`j')
		mat coefc`j' = coef`j'[A..A,1..2]
		mat pvalc`j' = pval`j'[A..A,1..1]
		mat coef`j' = coef`j'[1..R,1..2]
		mat pval`j' = pval`j'[1..R,1..1]
		}
	mat obs = obs1,obs2,obs3,obs4
	clear
	forval i = 1/4 {
		svmat coef`i'
		}
	forval i = 1/4 {
		svmat pval`i'
		}
	gen names = ""
	local names : rownames coef4
	local k = 1
	foreach j of local names {
		replace names = "`j'" in `k'
		local k = `k' + 1
		}
	egen base = rowtotal(coef*)
	drop if base == 0
	drop base
	local m 2 3 4 6 7 8 9 10 11 12 13 14 15 16 17 18 19 21 22 23 24 25 26 28 29 31 32 33 34 35
	gen order = .
	local k = 1
	foreach j of local m {
		replace order = `j' in `k'
		local k = `k' + 1
		}
	local n = _N
	local newn = `n'+5
	set obs `newn'
	local m 1 5 20 27 30
	local k = 1
	foreach j of local m {
		local newn = `n'+`k'
		replace order = `j' in `newn'
		local k = `k' + 1
		}
	sort order
	local n = _N
	local newn = `n'+1
	set obs `newn'
	forval i = 1/4 {
		replace coef`i'1 = coefc`i'[1,1] in `newn'
		replace coef`i'2 = coefc`i'[1,2] in `newn'
		replace pval`i'1 = pvalc`i'[1,1] in `newn'
		}
	mkmat coef*, mat(coef)
	forval i = 1/4 {
		gen empty`i' = .
		order empty`i', after(pval`i')
		}
	mkmat pval1-empty4, mat(pval)
	local cols = colsof(pval)
	local rows = rowsof(pval)
	mat stars = J(`rows',`cols',0)
	forval i = 1/`rows' {
		forval j = 1/`cols' {
			local pval = pval[`i',`j']
		if `pval' <= .10 & `pval' > .05 {
			matrix stars[`i',`j'] = 1
			}
		if `pval' <= .05 & `pval' > .01 {
			matrix stars[`i',`j'] = 2 
			}
		if `pval' <= .01 {
			matrix stars[`i',`j'] = 3
			}
		}
	}
	frmttable, clear
	frmttable, statmat(coef) substat(0) sdec(2) annotate(stars) asymbol(*,**,***) ///
		rtitles("\textbf{Economic class}" \ "\hspace{1em} Highly affluent (top 1\%)" \  ///
		"\hspace{1em} Affluent (95-99\%)" \ "\hspace{1em} Upper middle class ($\sim$80-95\%)" \ ///
		"\textbf{Socio-demographic controls}" \ "\hspace{1em} Age" \ ///
		"\hspace{1em} Female" \ "\hspace{1em} White" \ "\hspace{1em} Retired" \ ///
		"\hspace{1em} Married" \ "\hspace{1em} Education--High school" \ ///
		"\hspace{1em} Education--Some college" \ "\hspace{1em} Education--2-year college" \ ///
		"\hspace{1em} Education--4-year college" \ "\hspace{1em} Education--Post-grad" \ ///
		"\hspace{1em} Region--Northeast" \ "\hspace{1em} Region--Midwest" \ ///
		"\hspace{1em} Region--West" \ "\hspace{1em} Church attendance" \ ///
		"\textbf{Attitudinal controls}" \ ///
		"\hspace{1em} Evolution--Evolved, natural selection" \ "\hspace{1em} Evolution--Evolved, supreme being" \ ///
		"\hspace{1em} Evolution--Existed in present state" \ "\hspace{1em} Trust in science" \ ///
		"\hspace{1em} Racial segregation" \ "\hspace{1em} Fairness to Whites" \ ///
		"\textbf{Party ID}" \ "\hspace{1em} Democrat" \ "\hspace{1em} Republican" \ ///
		"\textbf{Additional controls}" \ "\hspace{1em} Political interest--Most of time" \ ///
		"\hspace{1em} Political interest--Some of time" \ "\hspace{1em} Political interest--Now and then" \ ///
		"\hspace{1em} Wealth source--Own business" \ "\hspace{1em} Wealth source--Inheritance" \ ///
		"Constant")
	frmttable, statmat(obs) substat(0) sdec(0) rtitle("N") append
end

* capture both 95 and 90 CIs for graphs
program def ci9590
	args m /* number of attribution vars */
	$cond
	forval i = 1/`m' {
		mat `: word `i' of ${vars}' = J(3,1,.)
		mat `: word `i' of ${vars}'95 = J(3,2,.)
		mat `: word `i' of ${vars}'90 = J(3,2,.)
		}
	forval i = 1/`m' {
		qui reg policy ($vars1)##i.t15r $controls1 $add $weight, r
		qui margins, dydx(`: word `i' of ${vars}') at(t15r = (1 2 3)) post
		mat `: word `i' of ${vars}' = e(b)'
		foreach l in 95 90 {
			qui reg policy ($vars1)##i.t15r $controls1 $add $weight, r
			qui margins, dydx(`: word `i' of ${vars}') at(t15r = (1 2 3)) l(`l') post
			mat `: word `i' of ${vars}'`l' = r(table)'
			mat `: word `i' of ${vars}'`l' = `: word `i' of ${vars}'`l'[1..3,5..6]
			}
		}
	clear 
	forval i = 1/`m' {
		svmat `: word `i' of ${vars}'
		foreach l in 95 90 {
			svmat `: word `i' of ${vars}'`l'
			}
		}
end

* interactive-analysis graph---first-order attributions
prog def foa_graph
	gen n = _n
	twoway (rcap disp951 disp952 n, msize(0) lcol(black) lwidth(vthin) horizontal) ///
		(rcap disp901 disp902 n, msize(0) lcol(black) lwidth(medthick) horizontal) ///
		(scatter n disp1, mcol(black) msize(large) msymbol(O)), ///
		aspect(1.3) xline(0, lpattern(dot) lcolor(gs8) lwidth(medthick)) ///
		legend(off) xtitle("") title("Dispositional") ytitle("") ///
		ylab(1 `""General" "population""' 2 `""Upper middle" " class (~80-95%)""' 3 "Top 5%", angle(0)) ///
		scheme(s1mono) yscale(range(.5(.5)3.5)) ///
		xscale(range(-.6(.2).2)) xlabel(#5) name(disp, replace) nodraw
	twoway (rcap sit951 sit952 n, msize(0) lcol(black) lwidth(vthin) horizontal) ///
		(rcap sit901 sit902 n, msize(0) lcol(black) lwidth(medthick) horizontal) ///
		(scatter n sit1, mcol(black) msize(large) msymbol(O)), ///
		aspect(1.3) ///
		legend(off) xtitle("") title("Situational") ytitle("") ///
		ylab(1 `""General" "population""' 2 `""Upper middle" " class (~80-95%)""' 3 "Top 5%", angle(0)) ///
		scheme(s1mono) yscale(range(.5(.5)3.5) alt) ///
		xscale(range(0.2(.2)1)) xlabel(#5) name(sit, replace) nodraw
	gr combine disp sit, rows(1) scheme(s1mono) ///
		b1("Association between one-unit increase" "in attribution and economic liberalism", size(small)) ///
		ysize(3) scale(1.35)
end

* interactive analysis graph---second-order attributions
prog def soa_graph
	gen n = _n
	twoway (rcap choice951 choice952 n, msize(0) lcol(black) lwidth(vthin) horizontal) ///
		(rcap choice901 choice902 n, msize(0) lcol(black) lwidth(medthick) horizontal) ///
		(scatter n choice1, mcol(black) msize(large) msymbol(O)), ///
		xline(0, lpattern(dot) lcolor(gs8) lwidth(medthick)) ///
		legend(off) xtitle("") title("Choice") ytitle("") ///
		ylab(1 `""General" "population""' 2 `""Upper middle" " class (~80-95%)""' 3 "Top 5%", angle(0)) ///
		scheme(s1mono) yscale(range(.5(.5)3.5)) ///
		xscale(range(-.6(.2).2)) xlabel(#5) name(choice, replace) nodraw
	twoway (rcap genes951 genes952 n, msize(0) lcol(black) lwidth(vthin) horizontal) ///
		(rcap genes901 genes902 n, msize(0) lcol(black) lwidth(medthick) horizontal) ///
		(scatter n genes1, mcol(black) msize(large) msymbol(O)), ///
		xline(0, lpattern(dot) lcolor(gs8) lwidth(medthick)) ///
		legend(off) xtitle("") title("Genes") ytitle("") ///
		ylab(, nolabel noticks) fxsize(35.5) xscale(range(-.4(.2).4)) xlabel(#5) ///
		scheme(s1mono) yscale(range(.5(.5)3.5) alt) name(genes, replace) nodraw
	twoway (rcap envir951 envir952 n, msize(0) lcol(black) lwidth(vthin) horizontal) ///
		(rcap envir901 envir902 n, msize(0) lcol(black) lwidth(medthick) horizontal) ///
		(scatter n envir1, mcol(black) msize(large) msymbol(O)), ///
		xline(0, lpattern(dot) lcolor(gs8) lwidth(medthick)) ///
		legend(off) xtitle("") title("Environment") ytitle("") ///
		ylab(1 `""General" "population""' 2 `""Upper middle" " class (~80-95%)""' 3 "Top 5%", angle(0)) ///
		scheme(s1mono) yscale(range(.5(.5)3.5) alt) ///
		xscale(range(-.4(.2).4)) xlabel(#5) name(envir, replace) nodraw
	gr combine choice genes envir, rows(1) scheme(s1mono) ///
		b1("Association between one-unit increase" "in attribution and economic liberalism", size(small)) ///
		scale(1.2) xsize(8) ysize(4.35)
end


/*-----------------------------------------------------------------------------*/
/* Figure A3: Objective and subjective measures of affluence */
/*-----------------------------------------------------------------------------*/

setup 
recode t15 (4 = 2) (2 = 4)
la def t15 1 `""General" "population""' 2 `""Upper middle" "class (~80-95%)""' ///
	3 `""Affluent" "(95-99%)""' 4 `""Highly affluent" "(top 1%)""', replace
la val t15 t15
hist inc_self, by(t15, row(1) note("")) percent scheme(s1mono) ///
	xtitle("Self-placement on 0-100 income scale") color(gs6)

/*-----------------------------------------------------------------------------*/
/* FULL COEFFICIENT TABLES FOR MAIN TEXT RESULTS */
/*-----------------------------------------------------------------------------*/

* Table A4: Affluence and first-order attributions---full results
setup
local k = 1
foreach y in disp sit foa_diff {
	getcoef `y' `k' wt
	local k = `k' + 1
	}
foreach a in coef obs {
	coefmerge `a' 3
	}
mat pval = pval1,empty1,pval2,empty2,pval3,empty3
matclean
getstars pval stars
tablel

* Table A5: Affluence and second-order attributions---full results
setup
local k = 1
foreach y in choice genes envir dd {
	getcoef `y' `k' wt
	local k = `k' + 1
	}
foreach a in coef obs {
	coefmerge `a' 4
	}
mat pval = pval1,empty1,pval2,empty2,pval3,empty3,pval4,empty4
matclean
getstars pval stars
tablel

* Table A6: Affluence and political attitudes---full results
setup
local k = 1
foreach y in ineq govt taxes policy {
	getcoef `y' `k' wt
	local k = `k' + 1
	}
foreach a in coef obs {
	coefmerge `a' 4
	}
mat pval = pval1,empty1,pval2,empty2,pval3,empty3,pval4,empty4
matclean
getstars pval stars
tablel

* Table A7: Affluence, first-order attributions, and political attitudes---full results
setup
local j = 1
foreach x in policy {
	qui reg `x' (c.disp c.sit)##i.t5 $controls1, r
	mat obs`j' = e(N),.
	mat coef`j' = e(b)
	mat var`j' = vecdiag(e(V))
	matmap var`j' se`j', m(sqrt(@))
	mat pval`j' = r(table)
	local C = colsof(pval`j')
	mat pval`j' = pval`j'[4..4,1..`C']
	mat pval`j' = pval`j''
	mat coef`j' = coef`j'', se`j''
	local j = `j' + 1
	}
foreach a in coef obs pval {
	coefmerge `a' 1
	}
clear
svmat coef
svmat pval
gen names = ""
local names : rownames coef
local k = 1
foreach j of local names {
	replace names = "`j'" in `k'
	local k = `k' + 1
	}
egen base = rowtotal(coef*)
drop if base == 0
drop base
local m 2 3 5 6 8 9 10 11 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
gen order = .
local k = 1
foreach j of local m {
	replace order = `j' in `k'
	local k = `k' + 1
	}
local n = _N
local newn = `n'+4
set obs `newn'
local m 1 4 7 12
local k = 1
foreach j of local m {
	local newn = `n'+`k'
	replace order = `j' in `newn'
	local k = `k' + 1
	}
sort order
mkmat coef*, mat(coef)
forval i = 1/1 {
	gen empty`i' = .
	order empty`i', after(pval`i')
	}
mkmat pval1-empty1, mat(pval)
getstars pval stars

frmttable, clear
frmttable, statmat(coef) substat(0) sdec(2) annotate(stars) asymbol(*,**,***) ///
	rtitles("\textbf{Attributions}" \ "\hspace{1em} Dispositional (work, IQ)" \ ///
	"\hspace{1em} Situational (luck, family)" \ "\textbf{Economic class}" \ ///
	"\hspace{1em} Top 5\%" \ "\hspace{1em} Upper middle class ($\sim$80-95\%)" \ ///
	"\textbf{Attributions $\times$ economic class}" \ ///
	"\hspace{1em} Dispositional $\times$ top 5\%" \ ///	
	"\hspace{1em} Dispositional $\times$ upper middle class" \ ///
	"\hspace{1em} Situational $\times$ top 5\%" \ ///	
	"\hspace{1em} Situational $\times$ upper middle class" \ ///
	"\textbf{Socio-demographic controls}" \ "\hspace{1em} Age" \ ///
	"\hspace{1em} Female" \ "\hspace{1em} White" \ "\hspace{1em} Retired" \ ///
	"\hspace{1em} Married" \ "\hspace{1em} Education--High school" \ ///
	"\hspace{1em} Education--Some college" \ "\hspace{1em} Education--2-year college" \ ///
	"\hspace{1em} Education--4-year college" \ "\hspace{1em} Education--Post-grad" \ ///
	"\hspace{1em} Region--Northeast" \ "\hspace{1em} Region--Midwest" \ "\hspace{1em} Region--West" \ ///
	"\hspace{1em} Church attendance" \ "Constant")
frmttable, statmat(obs) substat(0) sdec(0) rtitle("N") append

* Table A8: Affluence, second-order attributions, and political attitudes---full results
setup
local j = 1
foreach x in policy {
	qui reg `x' (c.choice c.genes c.envir)##i.t5 $controls1, r
	mat obs`j' = e(N),.
	mat coef`j' = e(b)
	mat var`j' = vecdiag(e(V))
	matmap var`j' se`j', m(sqrt(@))
	mat pval`j' = r(table)
	local C = colsof(pval`j')
	mat pval`j' = pval`j'[4..4,1..`C']
	mat pval`j' = pval`j''
	mat coef`j' = coef`j'', se`j''
	local j = `j' + 1
	}
foreach a in coef obs pval {
	coefmerge `a' 1
	}
clear
svmat coef
svmat pval
gen names = ""
local names : rownames coef
local k = 1
foreach j of local names {
	replace names = "`j'" in `k'
	local k = `k' + 1
	}
egen base = rowtotal(coef*)
drop if base == 0
drop base
local m 2 3 4 6 7 9 10 11 12 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
gen order = .
local k = 1
foreach j of local m {
	replace order = `j' in `k'
	local k = `k' + 1
	}
local n = _N
local newn = `n'+4
set obs `newn'
local m 1 5 8 15
local k = 1
foreach j of local m {
	local newn = `n'+`k'
	replace order = `j' in `newn'
	local k = `k' + 1
	}
sort order
mkmat coef*, mat(coef)
forval i = 1/1 {
	gen empty`i' = .
	order empty`i', after(pval`i')
	}
mkmat pval1-empty1, mat(pval)
getstars pval stars
frmttable, clear
frmttable, statmat(coef) substat(0) sdec(2) annotate(stars) asymbol(*,**,***) ///
	rtitles("\textbf{Attributions}" \ "\hspace{1em} Choice" \ ///
	"\hspace{1em} Genes" \ "\hspace{1em} Environment"  \ "\textbf{Economic class}" \ ///
	"\hspace{1em} Top 5\%" \ "\hspace{1em} Upper middle class ($\sim$80-95\%)" \ ///
	"\textbf{Attributions $\times$ economic class}" \ ///
	"\hspace{1em} Choice $\times$ top 5\%" \ ///
	"\hspace{1em} Choice $\times$ upper middle class" \ ///
	"\hspace{1em} Genes $\times$ top 5\%" \ ///
	"\hspace{1em} Genes $\times$ upper middle class" \ ///	
	"\hspace{1em} Environment $\times$ top 5\%" \ ///	
	"\hspace{1em} Environment $\times$ upper middle class" \ ///
	"\textbf{Socio-demographic controls}" \ "\hspace{1em} Age" \ ///
	"\hspace{1em} Female" \ "\hspace{1em} White" \ "\hspace{1em} Retired" \ ///
	"\hspace{1em} Married" \ "\hspace{1em} Education--High school" \ ///
	"\hspace{1em} Education--Some college" \ "\hspace{1em} Education--2-year college" \ ///
	"\hspace{1em} Education--4-year college" \ "\hspace{1em} Education--Post-grad" \ ///
	"\hspace{1em} Region--Northeast" \ "\hspace{1em} Region--Midwest" \ "\hspace{1em} Region--West" \ ///
	"\hspace{1em} Church attendance" \ "Constant")
frmttable, statmat(obs) substat(0) sdec(0) rtitle("N") append
	
/*-----------------------------------------------------------------------------*/
/* RESULTS WITH ADDITIONAL CONTROLS (SELECTED) */
/*-----------------------------------------------------------------------------*/

* Table A9: Affluence and dispositional first-order attributions---additional speciifications
addcons disp

* Table A10: Affluence and dispositional vs. situational first-order attributions---additional specifications
addcons foa_diff
	
* Table A11: Affluence and choice second-order attributions---additional specifications
addcons choice
	
* Table A12: Affluence and genetic second-order attributions---additional specifications
addcons genes

/*-----------------------------------------------------------------------------*/
/* POLICY/ATTRIBUTIONS RESULTS WITH INTERACTIONS WITH POLITICAL INTEREST */
/*-----------------------------------------------------------------------------*/

setup
global controls1 age female white retired married i.educ i.newsint b3.region pew_churatd

* Figure A4: Affluence, first-order attributions, and political attitudes
*---adding attribution interactions with political interest
global add (c.disp c.sit)##i.newsint
global vars disp sit
global vars1 c.disp c.sit
ci9590 2
foa_graph
	
* Figure A5: Affluence, second-order attributions, and political attitudes
*---adding attribution interactions with political interest
setup
global add (c.choice c.genes c.envir)##i.newsint
global vars choice genes envir
global vars1 c.choice c.genes c.envir
ci9590 3
soa_graph

/*-----------------------------------------------------------------------------*/
/* ATTITUDES/ATTRIBUTIONS RESULTS WITH BINNED ATTRIBUTION VARIABLES */
/*-----------------------------------------------------------------------------*/

setup
* 3-point scales
foreach x in disp sit choice genes envir {
	gen `x'i = 1 if `x' < 4
	replace `x'i = 2 if `x' >= 4 & `x' < 6
	replace `x'i = 3 if `x' >= 6 & `x' ~= .
	}
	
* Table A13: Affluence, first-order attributions, and political attitudes---alternative analysis
preserve
reg policy i.t5##(i.dispi i.siti) $controls1, r
mat obs = e(N),.
mat coef = e(b)
mat var = vecdiag(e(V))
matmap var se, m(sqrt(@))
mat pval = r(table)
local C = colsof(pval)
mat pval = pval[4..4,1..`C']
mat pval = pval'
mat coef = coef', se'
clear
svmat coef
svmat pval
gen names = ""
local names : rownames coef
local k = 1
foreach j of local names {
	replace names = "`j'" in `k'
	local k = `k' + 1
	}
egen base = rowtotal(coef*)
drop if base == 0
drop base
keep in 1/14
local m 2 3 5 6 7 8 10 11 12 13 14 15 16 17
gen order = .
local k = 1
foreach j of local m {
	replace order = `j' in `k'
	local k = `k' + 1
	}
local n = _N
local newn = `n'+3
set obs `newn'
local m 1 4 9
local k = 1
foreach j of local m {
	local newn = `n'+`k'
	replace order = `j' in `newn'
	local k = `k' + 1
	}
sort order
mkmat coef*, mat(coef)
gen empty1 = .
order empty1, after(pval1)
mkmat pval1-empty1, mat(pval)
getstars pval stars
frmttable, clear
frmttable, statmat(coef) substat(0) sdec(2) annotate(stars) asymbol(*,**,***) ///
	rtitles("\textbf{Economic class}" \ "\hspace{1em} Top 5\%" \ ///
	"\hspace{1em} Upper middle class ($\sim$80-95\%)" \ ///
	"\textbf{Attributions}" \ "\hspace{1em} Dispositional--medium" \ ///
	"\hspace{1em} Dispositional--high" \ "\hspace{1em} Situational--medium" \ ///
	"\hspace{1em} Situational--high" \ "\textbf{Attributions $\times$ economic class}" \ ///
	"\hspace{1em} Top 5\% $\times$ dispositional--medium" \ ///
	"\hspace{1em} Top 5\% $\times$ dispositional--high" \ ///
	"\hspace{1em} Upper middle class $\times$ dispositional--medium" \ ///
	"\hspace{1em} Upper middle class $\times$ dispositional--high" \ ///
	"\hspace{1em} Top 5\% $\times$ situational--medium" \ ///
	"\hspace{1em} Top 5\% $\times$ situational--high" \ ///
	"\hspace{1em} Upper middle class $\times$ situational--medium" \ ///
	"\hspace{1em} Upper middle class $\times$ situational--high")
frmttable, statmat(obs) substat(0) sdec(0) rtitle("N") append
restore

* Table A14: Affluence, second-order attributions, and political attitudes---alternative analysis
reg policy i.t5##(i.choicei i.genesi i.enviri) $controls1, r
mat obs = e(N),.
mat coef = e(b)
mat var = vecdiag(e(V))
matmap var se, m(sqrt(@))
mat pval = r(table)
local C = colsof(pval)
mat pval = pval[4..4,1..`C']
mat pval = pval'
mat coef = coef', se'
clear
svmat coef
svmat pval
gen names = ""
local names : rownames coef
local k = 1
foreach j of local names {
	replace names = "`j'" in `k'
	local k = `k' + 1
	}
egen base = rowtotal(coef*)
drop if base == 0
drop base
keep in 1/20
local m 2 3 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 22 23
gen order = .
local k = 1
foreach j of local m {
	replace order = `j' in `k'
	local k = `k' + 1
	}
local n = _N
local newn = `n'+3
set obs `newn'
local m 1 4 11
local k = 1
foreach j of local m {
	local newn = `n'+`k'
	replace order = `j' in `newn'
	local k = `k' + 1
	}
sort order
mkmat coef*, mat(coef)
gen empty1 = .
order empty1, after(pval1)
mkmat pval1-empty1, mat(pval)
getstars pval stars
frmttable, clear
frmttable, statmat(coef) substat(0) sdec(2) annotate(stars) asymbol(*,**,***) ///
	rtitles("\textbf{Economic class}" \ "\hspace{1em} Top 5\%" \ ///
	"\hspace{1em} Upper middle class ($\sim$80-95\%)" \ ///
	"\textbf{Attributions}" \ "\hspace{1em} Choice--medium" \ ///
	"\hspace{1em} Choice--high" \ "\hspace{1em} Genes--medium" \ "\hspace{1em} Genes--high" \ ///
	"\hspace{1em} Environment--medium" \ "\hspace{1em} Environment--high" \ ///
	"\textbf{Attributions $\times$ economic class}" \ ///
	"\hspace{1em} Top 5\% $\times$ choice--medium" \ ///
	"\hspace{1em} Top 5\% $\times$ choice--high" \ ///
	"\hspace{1em} Upper middle class $\times$ choice--medium" \ ///
	"\hspace{1em} Upper middle class $\times$ choice--high" \ ///
	"\hspace{1em} Top 5\% $\times$ genes--medium" \ ///
	"\hspace{1em} Top 5\% $\times$ genes--high" \ ///
	"\hspace{1em} Upper middle class $\times$ genes--medium" \ ///
	"\hspace{1em} Upper middle class $\times$ genes--high" \ ///
	"\hspace{1em} Top 5\% $\times$ environment--medium" \ ///
	"\hspace{1em} Top 5\% $\times$ environment--high" \ ///
	"\hspace{1em} Upper middle class $\times$ environment--medium" \ ///
	"\hspace{1em} Upper middle class $\times$ environment--high")
frmttable, statmat(obs) substat(0) sdec(0) rtitle("N") append
	
/*-----------------------------------------------------------------------------*/
/* WEIGHTED RESULTS */
/*-----------------------------------------------------------------------------*/

* Table A16: Affluence and first-order attributions---weighted results
setup
local k = 1
foreach y in disp sit foa_diff {
	getcoef `y' `k' weight
	local k = `k' + 1
	}
foreach a in coef obs {
	coefmerge `a' 3
	}
mat pval = pval1,empty1,pval2,empty2,pval3,empty3
matclean
getstars pval stars
tablel

* Table A17: Affluence and second-order attributions---weighted results
setup
local k = 1
foreach y in choice genes envir dd {
		getcoef `y' `k' weight
		local k = `k' + 1
		}
foreach a in coef obs {
	coefmerge `a' 4
	}
mat pval = pval1,empty1,pval2,empty2,pval3,empty3,pval4,empty4
matclean
getstars pval stars
tablel
	
* Figure A6: Affluence, first-order attributions, and political attitudes---weighted results
setup
global cond drop if weight > 3.5
global weight [pw=weight]
global add 
global vars disp sit
global vars1 c.disp c.sit
ci9590 2
foa_graph

* Figure A7: Affluence, second-order attributions, and political attitudes---weighted results
setup
global cond drop if weight > 3.5
global weight [pw=weight]
global vars choice genes envir
global vars1 c.choice c.genes c.envir
ci9590 3
soa_graph
	
